/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainMenu.java
 *
 * Created on Jul 29, 2011, 1:23:52 PM
 */
package deatax.display.gui;

import deatax.mode.GameMode;
import deatax.network.Server;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author pson
 */
public class MainMenu extends javax.swing.JPanel {

    private boolean is_host = false;
    ActionListener playListener;

    /** Creates new form MainMenu */
    public MainMenu() {
        initComponents();
        pnlPlayGame.addPlayListener(new MainMenuGameListener());
    }

    private void closePanel(int n) {
        if (mainPanel.getComponent(n) instanceof HostInitPanel) {
            ((HostInitPanel) mainPanel.getComponent(n)).close();
        } else if (mainPanel.getComponent(n) instanceof HotSeatInitPanel) {
            ((HotSeatInitPanel) mainPanel.getComponent(n)).close();
        } else if (mainPanel.getComponent(n) instanceof JoinInitPanel) {
            ((JoinInitPanel) mainPanel.getComponent(n)).close();
        }
        mainPanel.remove(n);
    }

    private void openPanel(Component c, int n) {
        mainPanel.add(c, n);
        if (c instanceof HostInitPanel) {
            ((HostInitPanel) c).open();
        } else if (c instanceof HotSeatInitPanel) {
            ((HotSeatInitPanel) c).open();
        } else if (c instanceof JoinInitPanel) {
            ((JoinInitPanel) c).open();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        pnlHotInit = new deatax.display.gui.HotSeatInitPanel();
        pnlHostInit = new deatax.display.gui.HostInitPanel();
        pnlPlayGame = new deatax.display.gui.PlayGamePanel();
        pnlJoinInit = new deatax.display.gui.JoinInitPanel();
        mainPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        btnExit = new javax.swing.JButton();
        btnHot = new javax.swing.JButton();
        btnHost = new javax.swing.JButton();
        btnJoin = new javax.swing.JButton();
        btnOption = new javax.swing.JButton();
        pnlBlank2 = new javax.swing.JPanel();
        pnlBlank3 = new javax.swing.JPanel();
        banner = new deatax.display.gui.ImagePanel();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 195, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 289, Short.MAX_VALUE)
        );

        setLayout(new java.awt.BorderLayout(20, 20));

        mainPanel.setLayout(new java.awt.GridLayout(1, 3, 20, 0));

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        btnHot.setText("Hot Seat");
        btnHot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHotActionPerformed(evt);
            }
        });

        btnHost.setText("Host Game");
        btnHost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHostActionPerformed(evt);
            }
        });

        btnJoin.setText("Join Game");
        btnJoin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnJoinActionPerformed(evt);
            }
        });

        btnOption.setText("Options");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnHot, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addComponent(btnHost, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addComponent(btnJoin, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addComponent(btnOption, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnHot)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnHost)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnJoin)
                .addGap(18, 18, 18)
                .addComponent(btnOption)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExit)
                .addContainerGap(73, Short.MAX_VALUE))
        );

        mainPanel.add(jPanel1);

        javax.swing.GroupLayout pnlBlank2Layout = new javax.swing.GroupLayout(pnlBlank2);
        pnlBlank2.setLayout(pnlBlank2Layout);
        pnlBlank2Layout.setHorizontalGroup(
            pnlBlank2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 199, Short.MAX_VALUE)
        );
        pnlBlank2Layout.setVerticalGroup(
            pnlBlank2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 235, Short.MAX_VALUE)
        );

        mainPanel.add(pnlBlank2);

        javax.swing.GroupLayout pnlBlank3Layout = new javax.swing.GroupLayout(pnlBlank3);
        pnlBlank3.setLayout(pnlBlank3Layout);
        pnlBlank3Layout.setHorizontalGroup(
            pnlBlank3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 199, Short.MAX_VALUE)
        );
        pnlBlank3Layout.setVerticalGroup(
            pnlBlank3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 235, Short.MAX_VALUE)
        );

        mainPanel.add(pnlBlank3);

        add(mainPanel, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout bannerLayout = new javax.swing.GroupLayout(banner);
        banner.setLayout(bannerLayout);
        bannerLayout.setHorizontalGroup(
            bannerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 638, Short.MAX_VALUE)
        );
        bannerLayout.setVerticalGroup(
            bannerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        add(banner, java.awt.BorderLayout.NORTH);
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnHotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHotActionPerformed
        closePanel(1);
        openPanel(pnlHotInit, 1);
        closePanel(2);
        openPanel(pnlPlayGame, 2);
        mainPanel.validate();
        is_host = true;
        repaint();
    }//GEN-LAST:event_btnHotActionPerformed

    private void btnHostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHostActionPerformed
        closePanel(1);
        openPanel(pnlHostInit, 1);
        closePanel(2);
        openPanel(pnlPlayGame, 2);
        mainPanel.validate();
        is_host = true;
        repaint();
    }//GEN-LAST:event_btnHostActionPerformed

    private void btnJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnJoinActionPerformed
        closePanel(1);
        openPanel(pnlJoinInit, 1);
        closePanel(2);
        openPanel(pnlBlank3, 2);
        mainPanel.validate();
        is_host = false;
        repaint();
    }//GEN-LAST:event_btnJoinActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private deatax.display.gui.ImagePanel banner;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnHost;
    private javax.swing.JButton btnHot;
    private javax.swing.JButton btnJoin;
    private javax.swing.JButton btnOption;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel pnlBlank2;
    private javax.swing.JPanel pnlBlank3;
    private deatax.display.gui.HostInitPanel pnlHostInit;
    private deatax.display.gui.HotSeatInitPanel pnlHotInit;
    private deatax.display.gui.JoinInitPanel pnlJoinInit;
    private deatax.display.gui.PlayGamePanel pnlPlayGame;
    // End of variables declaration//GEN-END:variables

    public int getSelfPlayer() {
        Component c = mainPanel.getComponent(1);
        if (c instanceof HostInitPanel) {
            return 0;
        } else if (c instanceof JoinInitPanel) {
            return pnlJoinInit.getN() - 1;
        }
        return -1;
    }

    private class MainMenuGameListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (Server.get() != null) {
                Server.get().kill();
            }
            Component c = mainPanel.getComponent(1);
            if (c instanceof HostInitPanel) {
                pnlHostInit.assignPlayers();
                pnlPlayGame.assignParams();
            } else if (c instanceof HotSeatInitPanel) {
                pnlHotInit.assignPlayers();
                pnlPlayGame.assignParams();
            }
        }
    }
}
